package com.jp.vn.main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.jp.vn.Object.Data;
import com.jp.vn.Object.Question;

public class GreetingPractiseActivity extends BaseActivity {

	Random r = new Random();
	ArrayList<Question> arrayQuestion = new ArrayList<Question>();
	Question question;
	Vector<String> asw, v;
	int ans;
	Button Ans1, Ans2, Ans3, Ans4;
	TextView tfquestion, tfScore;
	static String correctAns = "";
	int idQues = 0;
	int score = 0;
	int clock = 0;
	Timer time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_greeting_practise);

		tfquestion = (TextView) findViewById(R.id.question);
		tfScore = (TextView) findViewById(R.id.tfScore);
		Ans1 = (Button) findViewById(R.id.ans1);
		Ans2 = (Button) findViewById(R.id.ans2);
		Ans3 = (Button) findViewById(R.id.ans3);
		Ans4 = (Button) findViewById(R.id.ans4);

		arrayQuestion = creatQuestions(Data.greeting.length);
		tfScore.setText(score + "");
		getQuestion(idQues);
		time = new Timer();
		time.scheduleAtFixedRate(new TimerTask() {         
		        @Override
		        public void run() {
		            runOnUiThread(new Runnable()
		            {
		                @Override
		                public void run()
		                {
		                   ++clock;
		                }
		            });
		        }
		    }, 1000, 1000);

		Ans1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				++idQues;
				if (Ans1.getText().equals(correctAns)) {
					++score;
					tfScore.setText(score + "");
				}
				if (idQues != arrayQuestion.size()) {
					getQuestion(idQues);
				} else {
					time.cancel();
					int wrong = arrayQuestion.size()- score;
					showDialogScore(GreetingPractiseActivity.this, clock, score,wrong);
				}
			}
		});

		Ans2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				++idQues;

				if (Ans2.getText().equals(correctAns)) {
					++score;
					tfScore.setText(score + "");
				}
				if (idQues != arrayQuestion.size()) {
					getQuestion(idQues);
				} else {
					time.cancel();
					int wrong = arrayQuestion.size()- score;
					showDialogScore(GreetingPractiseActivity.this, clock, score,wrong);
				}

			}
		});

		Ans3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				++idQues;
				if (Ans3.getText().equals(correctAns)) {
					++score;
					tfScore.setText(score + "");
				}
				if (idQues != arrayQuestion.size()) {
					getQuestion(idQues);
				} else {
					time.cancel();
					int wrong = arrayQuestion.size()- score;
					showDialogScore(GreetingPractiseActivity.this, clock, score,wrong);
				}
			}
		});

		Ans4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				++idQues;
				if (Ans4.getText().equals(correctAns)) {
					++score;
					tfScore.setText(score + "");
				}
				if (idQues != arrayQuestion.size()) {
					getQuestion(idQues);
				} else {
					time.cancel();
					int wrong = arrayQuestion.size()- score;
					showDialogScore(GreetingPractiseActivity.this, clock, score,wrong);
				}
			}
		});
	}

	public ArrayList<Question> creatQuestions(int length) {
		ArrayList<Question> arrayQues = new ArrayList<Question>();
		for (int i = 0; i < length; i++) {
			question = new Question();
			question.setQuestion(Data.greetingLatin[i]);
			question.setAnswerCorrect(Data.greeting[i]);

			asw = randomAnswer(question.getAnswerCorrect());
			question.setAnswerA(asw.get(0));
			question.setAnswerB(asw.get(1));
			question.setAnswerC(asw.get(2));
			question.setAnswerD(asw.get(3));

			arrayQues.add(question);
			Collections.shuffle(arrayQues);
		}
		return arrayQues;
	}

	public Vector<String> randomAnswer(String correctAnswer) {
		v = new Vector<String>();
		v.add(correctAnswer);
		do {
			ans = r.nextInt(Data.greeting.length);
			String tmp = Data.greeting[ans];
			if (!v.contains(tmp)) {
				v.add(tmp);
			}
		} while (v.size() < 4);

		Collections.shuffle(v);
		return v;
	}

	public void getQuestion(int id) {
		tfquestion.setText(arrayQuestion.get(id).getQuestion());
		Ans1.setText(arrayQuestion.get(id).getAnswerA());
		Ans2.setText(arrayQuestion.get(id).getAnswerB());
		Ans3.setText(arrayQuestion.get(id).getAnswerC());
		Ans4.setText(arrayQuestion.get(id).getAnswerD());
		correctAns = arrayQuestion.get(id).getAnswerCorrect();
	}
}
